import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import {  Events,
          AlertController,
          App,
          ModalController,
          NavController,
          NavParams,
          ActionSheetController,
          Content,
          ViewController 
} from 'ionic-angular';
import { IrpReportInsideQueryService } from './query.service';
import { Config } from '../../../../../providers/config';
import { Common } from '../../../../../providers/common';
import {Irpreportmainpage} from '../main/main.component';
import _ from 'lodash';

@Component({
  selector: 'page-irp-report-query',
  templateUrl: './query.html',
  providers: [IrpReportInsideQueryService]
})
export class IrpReportInsideQueryPage {
  moa_page_name: any = "page-irp-report-query";
  moa_page_explain: any = "内部研报查询";
  public testCheckboxOpen: boolean;
  public testCheckboxResult; 
  // 默认查询条件
  public queryCondition = {
      keyWord : "",                     // 关键字
      authornames : "",                 // 报告作者
      bond : "",                        // 相关证券
      industry :"",                     // 相关行业
      reportType : "",                  // 报告类型
      brokername : "",                  // 来源机构
      startDate : "",                   // 开始日期
      endDate : "",                     // 结束日期
      industryLevel : "",               // 行业评级
      stockLevel : ""                   // 股票评级
  }; 
  // 可以选择的最大日期
  public maxDate = "";
  // 结束日期可以选择的最小日期
  public minEndDate = "1970-01-01";
  // 报告类型
  public reportTypeModel: Array<any> = [
    {name:"公司深度研究"},
    {name:"公司研究简报"},
    {name:"新股分析报告"},
    {name:"公司投资快讯"},
    {name:"行业深度研究"},
    {name:"行业策略报告"},
    {name:"股票融资分析报告"}
  ];
  // 来源机构
  public brokernameModel: Array<any> = [
    {name:"广发证券"},
    {name:"BCC"},
    {name:"北方证券"},
    {name:"渤海证券"},
    {name:"长城证券"},
    {name:"第一创业证券"},
    {name:"国泰君安期货"},
    {name:"安信证券"},
  ];
  // 相关行业
  public industryModel: Array<any> = [
    {name:"基金行业(0101)"},
    {name:"采矿业(0102)"},
    {name:"制造业(0103)"},
    {name:"建筑业(0104)"},
    {name:"采矿业(0105)"},
    {name:"综合(0106)"},
    {name:"教育(0107)"},
    {name:"农、林、牧、渔业(0108)"},
    {name:"批发和零售业(0109)"},
  ];
  // 相关行业option Title
  public industryAlertOpts: { 
      title: string
  };
  // 报告类型option Title
  public reportTypeAlertOpts: { 
    title: string
  };
  // 来源机构option Title
  public brokernameAlertOpts: { 
    title: string
  };
  
  
  constructor(public alertCtrl: AlertController,
              public app: App,
              public modalCtrl: ModalController,
              public navCtrl: NavController,
              private navParams: NavParams,
              public events: Events,
              public config: Config,
              public common: Common,
              public actionSheetCtrl: ActionSheetController,
              public viewCtrl: ViewController,
              public storage: Storage)
      {
      //this.queryCondition = this.defaultCondition
      this.storage.get("insidequeryCondition").then(data => {
          if (data != null) {
              this.queryCondition = data;
          }
      });
      //可以选择的日期不能超过今天
      this.maxDate = this.common.getNowFormatDate();
      this.industryAlertOpts= { 
        title: "相关行业"
      };
      this.reportTypeAlertOpts= { 
        title: "报告类型"
      };
      this.brokernameAlertOpts= { 
        title: "来源机构"
      };
    }
  //设置结束日期的最小可选日期等于起始日期
  ionChange(){
    this.minEndDate = this.queryCondition.startDate;
  }
  // 行业评级
  industryLevel() {
      let that = this;
      let alert = this.alertCtrl.create();
      alert.setTitle("行业评级");
      alert.addInput({
          type: 'checkbox',
          label: '超配',
          value: '超配'
      });
      alert.addInput({
          type: 'checkbox',
          label: '标配',
          value: '标配'
      });
      alert.addInput({
          type: 'checkbox',
          label: '低配',
          value: '低配'
      });
      alert.addButton('取消');
      alert.addButton({
          text: '确定',
          handler: data => {
              that.queryCondition.industryLevel = data;
              console.log(that.queryCondition.industryLevel);
              that.testCheckboxOpen = false;
              that.testCheckboxResult = data;
          }
      });
      alert.present().then(() => {
          that.testCheckboxOpen = true;
      });
  }
  // 股票评级
  stockLevel(){
    let that = this;
    let alert = this.alertCtrl.create();
    alert.setTitle("股票评级");
    alert.addInput({
        type: 'checkbox',
        label: '持有',
        value: '持有'
    });
    alert.addInput({
        type: 'checkbox',
        label: '减持',
        value: '减持'
    });
    alert.addInput({
        type: 'checkbox',
        label: '卖出',
        value: '卖出'
    });
    alert.addInput({
      type: 'checkbox',
      label: '买入',
      value: '买入'
    });
    alert.addInput({
      type: 'checkbox',
      label: '增持',
      value: '增持'
    });
    alert.addButton('取消');
    alert.addButton({
        text: '确定',
        handler: data => {
            that.queryCondition.stockLevel = data;
            console.log(this.queryCondition.stockLevel);
            that.testCheckboxOpen = false;
            that.testCheckboxResult = data;
        }
    });
    alert.present().then(() => {
        that.testCheckboxOpen = true;
    });
  }

  close(){
    this.viewCtrl.dismiss("close");
  }
  save(){
    this.storage.set("insidequeryCondition", this.queryCondition);
    console.log(this.queryCondition);
  }
  clear(){
    this.queryCondition = {
      keyWord : "",                     // 关键字
      authornames : "",                 // 报告作者
      bond : "",                        // 相关证券
      industry :"",                     // 相关行业
      reportType : "",                  // 报告类型
      brokername : "",                  // 来源机构
      startDate : "",                   // 开始日期
      endDate : "",                     // 结束日期
      industryLevel : "",               // 行业评级
      stockLevel : ""                   // 股票评级
    }
    this.storage.remove("insidequeryCondition");
  }
}
